package com.example.myapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.myapp.note.NoteAdapter;
import com.example.myapp.note.NoteBean;
import com.example.myapp.note.SQLiteHelper;

import java.util.List;

public class NotepadActivity extends AppCompatActivity {
    ListView listNote;
    List<NoteBean> list;
    SQLiteHelper helper;
    NoteAdapter adapter;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1 && resultCode==2) {
            showQueryData();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad);
        ImageView ivAdd = (ImageView) findViewById(R.id.ivAdd);
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(NotepadActivity.this, RecordActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        listNote = (ListView) findViewById(R.id.listNote);
        helper = new SQLiteHelper(this);
        showQueryData();
    }
    private void showQueryData() {
        if (list != null) {
            list.clear();
        }
        list = helper.query();
        adapter = new NoteAdapter(this, list);
        listNote.setAdapter(adapter);
    }
}